using System;
using System.Collections.Generic;
using UnityEngine;

public class PrizesPopUpLayer : MonoBehaviour
{
	[Serializable]
	public class Winnings
	{
		public int coins;

		public int cash;

		public int tokens;

		public void Add(LeaderboardsAdapter.ClientPrize prize)
		{
			coins += prize.coins;
			cash += prize.cash;
			tokens += prize.tokens;
		}
	}

	[Serializable]
	public class WinningsParticle : Winnings
	{
		public Particle2D particle;

		public bool isActive(Winnings winnings)
		{
			return (coins > 0 && winnings.coins > 0) || (cash > 0 && winnings.cash > 0) || (tokens > 0 && winnings.tokens > 0);
		}
	}

	public Particle2D coinsParticle;

	public Particle2D tokensParticle;

	public Particle2D cashParticle;

	public UILabel subHeader;

	public UITable prizesTable;

	public GameObject prizeEntry;

	public UIScrollView prizesView;

	public GameObject leadDisplayWindow;

	public UIWidget dialogWidget;

	public int heightForSingleEntry = 420;

	public int heightForMultipleEntries = 512;

	private string preamble = "Congrats!";

	private List<WinningsEntry> winningsEntries = new List<WinningsEntry>();

	private Action onCompleteDelegate;

	public List<WinningsParticle> particles = new List<WinningsParticle>();

	private Winnings totalWinnings;

	private void OnEnable()
	{
		UpdateHeader();
		StopAllCoroutines();
		StartCoroutine(UITools.DoAlignToTopOnScroll(winningsEntries[0].GetComponent<UIWidget>(), prizesView, -30f));
		foreach (WinningsParticle particle in particles)
		{
			if (particle.isActive(totalWinnings))
			{
				particle.particle.Play();
			}
		}
	}

	private void OnDisable()
	{
		foreach (WinningsEntry winningsEntry in winningsEntries)
		{
			winningsEntry.gameObject.SetActive(false);
		}
		Action action = onCompleteDelegate;
		onCompleteDelegate = null;
		if (action != null)
		{
			action();
		}
	}

	public void Init(Dictionary<string, LeaderboardsAdapter.ClientPrize> compPrizes, Action onCompleteDelegate)
	{
		this.onCompleteDelegate = onCompleteDelegate;
		int num = 0;
		if (compPrizes.Count > 1)
		{
			dialogWidget.height = heightForMultipleEntries;
		}
		else
		{
			dialogWidget.height = heightForSingleEntry;
		}
		totalWinnings = new Winnings();
		foreach (KeyValuePair<string, LeaderboardsAdapter.ClientPrize> compPrize in compPrizes)
		{
			if (num >= winningsEntries.Count)
			{
				GameObject gameObject = NGUITools.AddChild(prizesTable.gameObject, prizeEntry);
				gameObject.name = num.ToString();
				winningsEntries.Add(gameObject.GetComponent<WinningsEntry>());
			}
			LeaderboardsAdapter.ClientPrize value = compPrize.Value;
			Debug.Log(winningsEntries[num]);
			winningsEntries[num].gameObject.SetActive(true);
			SettingsUI.LayeredSpriteColor colorForPrize = ScriptableObjectSingleton<SettingsUI>.instance.GetColorForPrize(value.index);
			winningsEntries[num].Init(compPrize.Value, compPrize.Key, colorForPrize.colorBck, colorForPrize.colorFront);
			totalWinnings.Add(compPrize.Value);
			num++;
		}
		for (int i = num; i < winningsEntries.Count; i++)
		{
			winningsEntries[i].gameObject.SetActive(false);
		}
		prizesTable.Reposition();
	}

	private void UpdateHeader()
	{
		string text = preamble + " " + GGFormat.RandomFrom("Jolly good show!", "Well done!", "Good job!", "Spiffing, old chap!", "Nice one!");
		UITools.ChangeText(subHeader, text);
	}
}
